/*公共的ajax请求。
url:不带basePath全路径，
项目上下文是basePath，
 type:请求方式,通常是post请求,
 parameter:参数 ，json类型
 fn回调，将返回结果交给调用方处理
 */
function publicAjax(url, type, parameter, fn, closeLoading, param) {
  if (typeof fn !== 'function') {
    flavrShowByTime("回调必须是个函数,function", null, "danger", false);
    return;
  }
  closeLoading ? null : loading();
  /* 是否反响代理 */
  $.ajax($.extend({
    url: url,
    type: type,
    data: parameter || {},
    success: function (data) {
      fn(data);
      if (data.ret === 500) {
        console.log("出错额。。。。500");
      } else if (data.ret === 404) {
        console.log("出错额。。。。404");
      } else if (data.ret === undefined) {
        location.href = basePath;
      }
      loading(true);
    },
    error: function (data) {
      console.log("出错la。。。。");
      console.log(data);
      loading(true);
    }
  }, param));
}

/*
* Loading动画
* */
function loading(isOver) {
  var loadingDiv = $('#loading-div');
  if (isOver) {
    loadingDiv.hide();
  } else {
    if (loadingDiv.length) {
      loadingDiv.show();
    } else {
      $('body').append('<div id="loading-div"><img src="' + basePath + '/public/images/loading.gif" alt="正在加载，请稍候……"></div>').append('<style>#loading-div {position: fixed;width: 100%;height: 100%;left: 0;top: 0;background: rgba(0,0,0,0.5);z-index: 99999;}#loading-div img {display: block;width: 200px;height: 200px;position: absolute;left: 50%;top: 50%;margin: -100px 0 0 -100px;}</style>');
    }
  }
}

/* 获取basePath */
function getBasePath() {
  //获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
  var curWwwPath = window.document.location.href;
  //获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
  var pathName = window.document.location.pathname;
  var pos = pathName === "/" ? (curWwwPath.length - 1) : curWwwPath.indexOf(pathName);
  //获取主机地址，如： http://localhost:8080
  var localhostPath = curWwwPath.substring(0, pos);
  //获取带"/"的项目名，如：/ems
  var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
  //获取项目的basePath   http://localhost:8080/ems/
  window.basePath = localhostPath + projectName + "/";
  return basePath;
}
getBasePath();

/* 提示信息框 */
function flavrShowByTime(msg, urls, color, isHref) {
  new $.flavr({
    content: msg,
    autoclose: true,
    closeOverlay: true,
    closeEsc: true,
    buttons: [{text: 'ok', style: color}],
    timeout: 3500,
    onClose: function () {
      if (isHref && urls != null) {
        window.location.href = urls;
      }
    }
  });
}
function flavrShow(msg, urls, color, isHref, fn) {
  new $.flavr({
    content: msg,
    autoclose: false,
    closeOverlay: true,
    closeEsc: true,
    buttons: [{text: '确认', style: 'info', action: fn},{text: '取消', style: 'primary'}],
    onClose: function () {
      if (isHref && urls != null) {
        window.location.href = urls;
      }
    }
  });
}

/*
* 根据参数名获取url中的参数
* name  参数名
* */
function GetQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return decodeURI(r[2]);
  return null;
}

// 保存两位小数
function fix2(string) {
  return parseFloat(string || 0).toFixed(2);
}

// 保存为整数或者 0
function fix0(string) {
  return parseInt(string || 0);
}

function fixStr(string) {
  return string || '';
}
function fixLine(string) {
  if (string == 0) return 0;
  return string || '-';
}


function fixLine2(string) {
  if (string == 0) return '0.00';
  return string ? parseFloat(string).toFixed(2) : '-';
}
function fixNull (string) {
  if (string && string == 'null') {
    return null;
  } else {
    return string;
  }
}

